<?php

namespace App\Console\Commands\SiteLocation;

use App\Models\Common\ConfigModel;
use App\Models\SiteLocation\SiteLocationWhiteListModel;
use Illuminate\Console\Command;

class CompatibleWithOldWhitelist extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'siteLocation:compatible_with_old_whitelist';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '兼容旧的选址白名单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $whitelistJson = ConfigModel::query()
            ->where('module', 'wanggui')
            ->where('name', 'regionalWhiteList')
            ->value('value');

        $whitelistArr = json_decode($whitelistJson, true);

        $insertData = [];
        foreach ($whitelistArr as $item) {
            $provinceCodes = [];
            foreach ($item['provinceCodeArr'] as $code) {
                $provinceCodes[$code] = [$code];
            }

            $insertData[] = [
                'rmsUserId' => $item['userId'],
                'appPower'  => json_encode(array_values($provinceCodes), JSON_UNESCAPED_UNICODE),
                'pcPower'   => json_encode([], JSON_UNESCAPED_UNICODE),
            ];
        }

        SiteLocationWhiteListModel::query()->insert($insertData);
    }
}
